# -*- coding: utf-8 -*-
#
# Author:: hanjunfeng
# Date:: 2022/7/7
import numpy


def get_ma_list(candles_data, ma_type, offset=0, ma_number=None):
    """
    :param candles_data: 从左到右按时间倒序排列的数据列表
    :param ma_type:
    :param offset: 当前时间向前偏移量，offset=0是实时MA，offset=n是从n开始的MA
    :param ma_number:
    :return:
    """

    ma_type = int(ma_type)

    # 数据长度不够时返回最大可求ma
    data_list_len = len(candles_data)
    if ma_type > data_list_len:
        ma_type = data_list_len
    if ma_number == None:
        # 计算最大可求的MA数量
        ma_number = len(candles_data) - ma_type + 1
    else:
        ma_number = 1
    ma_list = []
    for i in range(ma_number):
        every_data = candles_data[i + offset : ma_type + i + offset]
        close_price_list = []
        for i2 in range(len(every_data)):
            close_price_list.append(float(every_data[i2][4]))
        ma = sum(close_price_list) / ma_type
        ma_list.append(ma)
    return ma_list


def get_boll_list(candles_data, mid=20, std_multiple=2, offset=0, boll_num=None):
    ma_type = mid
    ma_number = boll_num
    ma_type = int(ma_type)
    if ma_number == None:
        # 计算最大可求数量
        ma_number = len(candles_data) - ma_type + 1
    else:
        ma_number = 1

    boll_list = []
    for i in range(ma_number):
        every_data = candles_data[i + offset : ma_type + i + offset]
        close_price_list = []
        for i2 in range(len(every_data)):
            close_price_list.append(float(every_data[i2][4]))
        ma = sum(close_price_list) / ma_type
        std = numpy.std(close_price_list)

        boll = ma
        up_boll = ma + std * std_multiple
        low_boll = ma - std * std_multiple
        boll_list.append([boll, up_boll, low_boll])
    return boll_list


if __name__ == "__main__":
    pass
